import axios from 'axios'
import { ref } from 'vue'

export default function useRequest<T>(url: string) {
  const loading = ref(true) // 是否正在加载中
  const result = ref<T>() // 结果数据
  const errorMsg = ref('') // 请求错误提示信息

  axios(url).then(
    response => {
      loading.value = false
      result.value = response.data
    },
    error => {
      loading.value = false
      errorMsg.value = error.message || '请求未知错误'
    }
  )

  return {
    loading,
    result,
    errorMsg
  }

}